home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  9.7 KB  |  392 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    main.c
  5. *    Initialization, cleaning up, main loop and some general routines
  6. *        
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *        
  15. **********************************************************************
  16. *    
  17. */ 
  18.  
  19. #include <Balloons.h>
  20.  
  21. #include "EMBL-Search.h"
  22. #include "EMBL-Search.rsrc.h"
  23.  
  24. /*
  25. ******************************* Prototypes ***************************
  26. */
  27.  
  28. #include "main.h"
  29. #include "util.h"
  30. #include "window.h"
  31. #include "menus.h"
  32. #include "print.h"
  33. #include "query.h"
  34. #include "checkapp.h"
  35. #include "events.h"
  36. #include "environment.h"
  37. #include "pref.h"
  38. #include "cd.h"
  39. #include "appleevents.h"
  40.  
  41. static void GetGlobalMouse(Point *mouse);
  42. static void MaintainCursor(Point mouse, RgnHandle cursRgn);
  43. static long GetSleep(void);
  44. static void MainLoop(void);
  45. static void InitDefaults(void);
  46. static void PrepMenus(void);
  47. static void PrepFonts(void);
  48. static void PrepCursors(void);
  49. static void InitThings(void);
  50. static void CleaningWoman(void);
  51.  
  52.  
  53. /*
  54. ********************************* Global variables *******************
  55. */
  56.  
  57. extern short        gResNo, gQueryNo;
  58. extern Prefs        gPrefs;
  59. extern ClickInfo    gLastClick;
  60. extern Boolean        gHasAppleEvents;
  61. extern Boolean    bErrorOccurred;
  62. extern Boolean        gHasBalloonHelp;
  63.  
  64. CursHandle    gCursor[8];                            /* cursors used                            */
  65. MenuHandle    gMenu[MENUNR];                        /* menu handles                            */
  66. Boolean        gQuitApplication = FALSE;        /* flag for quitting the program        */
  67. VolInfo        gCurrentCD;                            /* information about current CD        */
  68. DBInfo        gDBInfo[DB_NUM];                    /* holds all information about databases */
  69. IndexFiles    gFileList;                            /* array of index file names */
  70. Str255        gVolLabel;                            /* default volume name */
  71. FONTINFO        fMonaco,fSystem;                    /* font information                        */
  72. Boolean        gInBackground;                        /* is set if we´re currently in
  73.                                                             background */
  74.  
  75. static CursHandle    editCursor;                    /* actually not used */
  76.                                                             
  77.  
  78. /**************************************
  79. *    Find current mouse position in global coordinates
  80. *    Return value:    none
  81. *    Side-effect:    mouse position in *mouse
  82. */
  83.  
  84. static void GetGlobalMouse(Point *mouse)
  85. {
  86.     EventRecord theEvent;
  87.     
  88.     OSEventAvail(0, &theEvent);        /* no need to actually *get* events */
  89.     *mouse=theEvent.where;
  90.     
  91.     /* we could also call GetMouse plus LocalToGlobal, but that requires
  92.         that a valid port is set */
  93. }
  94.  
  95.  
  96. /**************************************
  97. *    Adjust cursor and recalculate cursor region for WNE.
  98. *    (Not really needed, we could easily set cursor region to NULL, as long as
  99. *  we don't have edit windows. But we include it to keep the structure of
  100. *  this sample code more general to facilitate further extension.)
  101. *    Return value:    none
  102. *    Side-effect:    new region in cursRgn
  103. */
  104.  
  105. static void MaintainCursor(Point mouse, RgnHandle cursRgn)
  106. {
  107.     RgnHandle    arrowRgn;
  108.     RgnHandle    editRgn;
  109.  
  110.     if (!gInBackground && !IsDAWindow(FrontWindow())) {
  111.         editRgn=NewRgn();
  112.         arrowRgn=NewRgn();    /* Now make it BIG */
  113.         SetRectRgn(arrowRgn,-32768,-32768,32766,32766); /* see TN.SC002 */
  114.         
  115.         /* calculate editRgn */
  116.         
  117.         /* in this example we don´t have any */
  118.                 
  119.         /* now subtract editRgn from arrowRgn */
  120.         
  121.         DiffRgn(arrowRgn,editRgn,arrowRgn);
  122.         
  123.         /* set cursor accordingly and set cursor region for next WNE call */
  124.  
  125.         if(PtInRgn(mouse,editRgn)) {
  126.             SetCursor(*editCursor);
  127.             CopyRgn(editRgn,cursRgn);
  128.         }
  129.         else {
  130.             InitCursor();
  131.             CopyRgn(arrowRgn,cursRgn);
  132.         }
  133.         
  134.         DisposeRgn(arrowRgn);
  135.         DisposeRgn(editRgn);
  136.     }
  137. }
  138.  
  139.  
  140. /**************************************
  141. *    Calculate value of 'sleep' for WaitNextEvent
  142. *    Return value:    sleep value
  143. */
  144.  
  145. static long GetSleep()
  146. {
  147.     return( (gInBackground) ? 0x7FFFFFFF : GetCaretTime() );
  148. }
  149.  
  150.  
  151. /**************************************
  152. *    This is the main loop, calling WaitNextEvent 
  153. */
  154.  
  155. static void MainLoop()
  156. {
  157.     EventRecord    theEvent;
  158.     RgnHandle    cursRgn=NewRgn();
  159.     Point            mouse;
  160.     Boolean        isEvent,isHandled;
  161.     
  162.     gInBackground=FALSE;
  163.     gLastClick.when=0L;                    /* initialize gLastClick */
  164.  
  165.     InitCursor();
  166.     GetGlobalMouse(&mouse);
  167.     MaintainCursor(mouse,cursRgn);
  168.     MaintainMenus();
  169.     
  170.     if(gPrefs.startQOpen && !IsAppWindow(FrontWindow()))
  171.         NewQuery();
  172.  
  173.     while(!gQuitApplication){    
  174.         /* if an error occurred while we were in the background show it now */
  175.         if( bErrorOccurred )
  176.             ErrorMsg(0);
  177.         
  178.         isEvent = WaitNextEvent(everyEvent,&theEvent,GetSleep(),cursRgn);
  179.         MaintainCursor(theEvent.where,cursRgn);
  180.         
  181.         if(theEvent.what == osEvt) /* work around bug in DialogSelect */
  182.             DoEvent(theEvent);
  183.         else {
  184.             /* special action needed if event is for a non-modal dialog window */
  185.             isHandled = FALSE;
  186.             if(IsDialogEvent(&theEvent))
  187.                 isHandled = DoQueryEvt(theEvent);
  188.                 
  189.             if(isEvent && !isHandled)
  190.                 DoEvent(theEvent);
  191.         }
  192.     }    /* end while */
  193.     DisposeRgn(cursRgn);
  194. }
  195.  
  196.  
  197. /**************************************
  198. *    Sets global variables to default values
  199. */
  200.  
  201. static void InitDefaults()
  202. {
  203.     *(gCurrentCD.volName) = EOS;
  204.     
  205.     GetIndString(gDBInfo[DB_EMBL].SeqDName,EMBLDATA,DATA_DIR);
  206.     GetIndString(gDBInfo[DB_EMBL].InxDName,EMBLDATA,INDEX_DIR);
  207.     GetIndString(gDBInfo[DB_SWISS].SeqDName,SWISSDATA,DATA_DIR);
  208.     GetIndString(gDBInfo[DB_SWISS].InxDName,SWISSDATA,INDEX_DIR);    
  209.     
  210.     GetIndString(gFileList.divFName,INDEX_NAMES,DIV_TABLE);
  211.     GetIndString(gFileList.enameIdxFName,INDEX_NAMES,ENAME_IDX);
  212.     GetIndString(gFileList.briefIdxFName,INDEX_NAMES,BRIEF_IDX);
  213.     GetIndString(gFileList.acnumTrgFName,INDEX_NAMES,ACNUM_TRG);
  214.     GetIndString(gFileList.acnumHitFName,INDEX_NAMES,ACNUM_HIT);
  215.     GetIndString(gFileList.keywTrgFName,INDEX_NAMES,KEYWORD_TRG);
  216.     GetIndString(gFileList.keywHitFName,INDEX_NAMES,KEYWORD_HIT);
  217.     GetIndString(gFileList.textTrgFName,INDEX_NAMES,FREETEXT_TRG);
  218.     GetIndString(gFileList.textHitFName,INDEX_NAMES,FREETEXT_HIT);
  219.     GetIndString(gFileList.authorTrgFName,INDEX_NAMES,AUTHOR_TRG);
  220.     GetIndString(gFileList.authorHitFName,INDEX_NAMES,AUTHOR_HIT);
  221.     GetIndString(gFileList.taxonTrgFName,INDEX_NAMES,TAXON_TRG);
  222.     GetIndString(gFileList.taxonHitFName,INDEX_NAMES,TAXON_HIT);
  223.     
  224.     GetIndString(gVolLabel,OTHERS,VOL_LABEL);
  225.     
  226.     gDBInfo[DB_EMBL].gDivNames = gDBInfo[DB_SWISS].gDivNames = NULL;
  227.     gResNo = gQueryNo = 0;
  228. }
  229.  
  230.  
  231. /**************************************
  232. *    Prepare our menus
  233. */
  234.  
  235. static void PrepMenus()
  236. {
  237.     register short i;
  238.     Str255 helpMenu;
  239.     MenuHandle mh;
  240.     
  241.     gMenu[APPLE]=GetMenu(APPLE_M);        
  242.     AddResMenu(gMenu[APPLE],'DRVR');        /* insert DAs in Apple-menu             */
  243.     
  244.     gMenu[FILE]=GetMenu(FILE_M);
  245.     gMenu[EDIT]=GetMenu(EDIT_M);
  246.     gMenu[OSTUFF]=GetMenu(OSTUFF_M);
  247.     gMenu[PREFS]=GetMenu(PREFS_M);
  248.     gMenu[WINDOWS]=GetMenu(WINDOWS_M);
  249.     gMenu[CREATOR]= GetMenu(CREATOR_M);
  250.     gMenu[FORMAT]=GetMenu(FORMAT_M);
  251.     gMenu[XREF] = GetMenu(XREF_M);
  252.     gMenu[POPUP] = GetMenu(POPUP_M);
  253.     
  254.     for(i=0;i <= WINDOWS;InsertMenu(gMenu[i++],0))
  255.         ;
  256.     InsertMenu(gMenu[CREATOR],-1);
  257.     InsertMenu(gMenu[FORMAT],-1);
  258.     InsertMenu(gMenu[XREF],-1);
  259.     InsertMenu(gMenu[POPUP],-1);
  260.  
  261.     if(gPrefs.creator == OTHERS_I)
  262.         UpdateOtherCreator();
  263.     CheckItem(gMenu[CREATOR],gPrefs.creator,TRUE);
  264.     
  265.     CheckItem(gMenu[FORMAT],gPrefs.format,TRUE);
  266.  
  267.     /* if we run under System 7 we also insert help into Help menu */
  268.     if(gHasBalloonHelp) {
  269.         if(HMGetHelpMenuHandle(&mh) == noErr) {
  270.             GetItem(gMenu[APPLE],HELP_I,helpMenu);
  271.             AppendMenu(mh,helpMenu);
  272.         }
  273.     }
  274.  
  275.     DrawMenuBar();
  276. }
  277.  
  278. /**************************************
  279. *    Get font information
  280. */
  281.  
  282. static void PrepFonts()
  283. {
  284.     TextSize(9);
  285.     GetFNum("\pMonaco",&fMonaco.num);
  286.     TextFont(fMonaco.num);
  287.     GetFontInfo(&fMonaco.finfo);
  288.     fMonaco.height=fMonaco.finfo.ascent+
  289.                         fMonaco.finfo.descent+
  290.                    fMonaco.finfo.leading;
  291.     TextSize(12);
  292.     TextFont(fSystem.num=0);
  293.     GetFontInfo(&fSystem.finfo);
  294.     fSystem.height=fSystem.finfo.ascent+
  295.                         fSystem.finfo.descent+
  296.                    fSystem.finfo.leading;
  297. }
  298.  
  299.  
  300. /**************************************
  301. *    Read in rotating cursor resources
  302. */
  303.  
  304. static void PrepCursors()
  305. {
  306.     register short i;
  307.     
  308.     editCursor = GetCursor(iBeamCursor);
  309.    gCursor[0] = GetCursor(watchCursor);
  310.    for(i=1;i<8;++i)
  311.        gCursor[i] = GetCursor(256+i);
  312.  
  313.    for(i=0;i<8;++i)
  314.        LockHandleHigh((Handle)gCursor[i]);
  315. }
  316.  
  317.  
  318. /**************************************
  319. *    Initialisation
  320. */
  321.  
  322. static void InitThings()
  323. {
  324.     register short    i;
  325.     EventRecord        event;
  326.     
  327.     MaxApplZone();
  328.     InitGraf(&thePort);
  329.     
  330.     for(i=0;i<10;++i)
  331.         MoreMasters();                        /* extra pointer blocks at  bottom of  heap */
  332.     
  333.     InitFonts();                            /* startup the font manager         */
  334.    InitWindows();                   /* startup the window manager     */
  335.    InitMenus();                     /* startup the menu manager         */
  336.    TEInit();                        /* startup the text edit manager    */
  337.    InitDialogs(NULL);               /* startup the dialog manager     */
  338.  
  339.     /*
  340.         This next bit of code is necessary to allow the default button of our
  341.         alert be outlined.
  342.     */
  343.      
  344.     for (i = 0; i < 3; ++i)
  345.         EventAvail(everyEvent, &event);
  346.  
  347.    CheckEnvironment();                    /* look for system version 6     */
  348.     PrepCursors();                            /* prepare cursors                 */
  349.     GetPrefs();
  350.     InitDefaults();                        /* set default values            */
  351.     PrepFonts();
  352.     PrepWindows();
  353.      PrepMenus();                            /* prepare menus                    */
  354.      PrepPrint();                            /* prepare printing */
  355.     InitCD();                                /* look for CD-ROM drive        */
  356.     if(gHasAppleEvents)
  357.          InitAppleEvents();
  358.  
  359. }
  360.  
  361.  
  362. /**************************************
  363. *    Tidy up
  364. */
  365.  
  366. static void CleaningWoman()
  367. {
  368.     register short i;
  369.     
  370.     /* release any previously occupied memory */
  371.     if(gDBInfo[DB_EMBL].gDivNames != NULL)
  372.         DisposHandle(gDBInfo[DB_EMBL].gDivNames);
  373.     if(gDBInfo[DB_SWISS].gDivNames != NULL)
  374.         DisposHandle(gDBInfo[DB_SWISS].gDivNames);
  375.     gDBInfo[DB_EMBL].gDivNames = gDBInfo[DB_SWISS].gDivNames = NULL;
  376.     
  377.    for(i=0;i < 8;++i)
  378.        HUnlock((Handle)gCursor[i]);
  379. }
  380.  
  381.  
  382. /**************************************
  383. *    Main
  384. */
  385.  
  386. void main()
  387. {
  388.     InitThings();                                /* prepare everything necessary            */
  389.     CheckAppFiles();
  390.     MainLoop();                                    /* here we do the work                        */
  391.     CleaningWoman();                            /* tidy up                                        */
  392. }